package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class SubOrganActivity extends AppCompatActivity {

    private String soid,name;
    private TextView tv;
    private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_organ);
        Bundle bundle=getIntent().getExtras();
        soid=bundle.getString("soid");
        name=bundle.getString("name");
        tv = findViewById(R.id.tv_sub_1);
        lv = findViewById(R.id.list_sub_view);
        tv.setText("欢迎,"+name);
        init();

    }
    private void init(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url="http://10.128.48.203:8000/sub-organization/?soid="+soid+"&username="+name;
                HttpURLConnection orgcon=null;
                try{
                    URL requesturl= new URL(url);
                    orgcon=(HttpURLConnection)requesturl.openConnection();
                    orgcon.setRequestMethod("GET");
                    orgcon.setReadTimeout(5000);
                    orgcon.connect();

                    if(orgcon.getResponseCode()==200){
                        InputStream inputs=orgcon.getInputStream();

                        //将二进制流包装
                        BufferedReader buf2 =new BufferedReader(new InputStreamReader(inputs));

                        //读取字符串
                        String line;
                        StringBuilder builder = new StringBuilder();
                        while((line=buf2.readLine())!=null){
                            builder.append(line);
                            builder.append("\n");
                        }
                        String res=builder.toString();
                        JSONObject jsonObject = new JSONObject(res);
                        JSONArray acts=jsonObject.getJSONArray("activities");
                        String acid,acname;
                        List<Suborg> suborgList = new ArrayList<>();
                        for(int i=0;i<acts.length();i++){
                            JSONObject sub=acts.optJSONObject(i);
                            acid=sub.optString("acid");
                            acname=sub.optString("acname");
                            Suborg sss = new Suborg(acname,acid);
                            suborgList.add(sss);
                        }
                        SuborgAdapter suborgAdapter= new SuborgAdapter(SubOrganActivity.this,R.layout.subo_layout,suborgList);
                        lv.setAdapter(suborgAdapter);

                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                                Suborg suborg= suborgList.get(position);
                                String acid=suborg.soid;
                                Intent intent1= new Intent(SubOrganActivity.this,ActActivity.class);
                                Bundle bundle =new Bundle();
                                bundle.putString("acid",acid);
                                bundle.putString("name",name);
                                intent1.putExtras(bundle);
                                startActivity(intent1);
                            }
                        });
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
}